﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PRE
{
    using BLL;
    using DAL.Entities;
    public partial class frmTaikhoan : Form
    {

        TaiKhoanBLL _taikhoanBLL = new BLL.TaiKhoanBLL();
        public frmTaikhoan()
        {
            TaiKhoanBLL _taikhoanBLL = new BLL.TaiKhoanBLL();
            InitializeComponent();
        }

       /* private void cmdClick_Click(object sender, EventArgs e)
        {
            var bll = new BLL.TaiKhoanBLL();
            dataGridView_Taikhoan.DataSource = bll.Select();
        }*/

        private void frmTaikhoan_Load(object sender, EventArgs e)
        {
            var bll = new BLL.TaiKhoanBLL();
            dataGridView_Taikhoan.DataSource = bll.Select();
            label6_Click(sender, e);
        }

        private void dataGridView_Taikhoan_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
           txtTendangnhap.Text = dataGridView_Taikhoan[0, e.RowIndex].Value + "";
            var tmp = (TaiKhoan)_taikhoanBLL.Select(txtTendangnhap.Text);
            if (tmp != null)           
            {
                txtMatkhau.Text = tmp.MAT_KHAU;
                txtQuyentruycap.Text = tmp.QUYEN_TRUY_CAP;
                
                
            }
        }

        private void cmdThem_Click(object sender, EventArgs e)
        {
            if (txtTendangnhap.Text + "" != "")
            {
                var o = new TaiKhoan() { TEN_DANG_NHAP = txtTendangnhap.Text, MAT_KHAU = txtMatkhau.Text, QUYEN_TRUY_CAP = txtQuyentruycap.Text };
                if (_taikhoanBLL.Insert(o) > 0) MessageBox.Show("them thanh cong!");
                else MessageBox.Show("Thêm không được");
            }
            else
            {
                MessageBox.Show("nhap ma doc gja!");
            }
            dataGridView_Taikhoan.DataSource = _taikhoanBLL.Select();
        }

        private void cmdLammoi_Click(object sender, EventArgs e)
        {
            txtTendangnhap.Focus();
            txtTendangnhap.Text = "";
            txtMatkhau.Text = "";
            txtQuyentruycap.Text = "";

        }

        private void label6_Click(object sender, EventArgs e)
        {
           int sql = dataGridView_Taikhoan.Rows.Count - 1;
           label5.Text = "Tổng số tài khoản là:  " + sql.ToString();
        }
    }
}
